package com.itboye.sunsun.luntan;

import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.beans.LuntanForumBean.LuntanForumItemBean;
import com.itboye.sunsunhome.R;

public class DragAdapter extends BaseAdapter {
	/** TAG */
	private final static String TAG = "DragAdapter";
	private boolean isItemShow = false;
	private Context context;
	private int holdPosition;
	private boolean isChanged = false;
	private boolean isListChanged = false;
	boolean isVisible = true;
	// public List<String> channelList;
	private TextView item_text;

	private ImageView icon_delete;
	public int remove_position = -1;
	
	RelativeLayout rl_subscribe;
	private boolean isUser = false;
	List<LuntanForumItemBean> channelList;
	OnClickListener deleteClickListner;

	public DragAdapter(Context context, List<LuntanForumItemBean> channelList,
			boolean isUser,OnClickListener deleteClickListner) {
		this.context = context;
		this.channelList = channelList;
		this.isUser = isUser;
		this.deleteClickListner=deleteClickListner;
	}

	/**
	 * 设置显示删除图标
	 * 
	 * @param isShowDelete
	 */
	// public void setIsShowDelete(boolean isShowDelete){
	// this.isChanged=isShowDelete;
	// this.notifyDataSetChanged();
	// }
	/**
	 * 获取是否已经显示删除图标
	 * 
	 * @return
	 */
	// public boolean getShowDelete(){
	// return isChanged;
	// }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return channelList == null ? 0 : channelList.size();
	}

	@Override
	public LuntanForumItemBean getItem(int position) {
		// TODO Auto-generated method stub
		if (channelList != null && channelList.size() != 0) {
			return channelList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.adapter_mygridview_item, null);
		item_text = (TextView) view.findViewById(R.id.text_item);
		icon_delete = (ImageView) view.findViewById(R.id.icon_delete);
		rl_subscribe=(RelativeLayout) view.findViewById(R.id.rl_subscribe);
		LuntanForumItemBean channel = getItem(position);
		item_text.setText(channel.getName());
		if (isUser) {
			if ((position == 0) || (position == 1)) {
				item_text.setEnabled(false);
			}
		}
		if (isChanged && (position == holdPosition) && !isItemShow) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
			isChanged = false;
		}
		if (!isVisible && (position == -1 + channelList.size())) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
		}
		if (remove_position == position) {
			item_text.setText("");
		}
		item_text.setTag(channelList.get(position).getDeleteStatus());
		icon_delete.setTag(position);
		if (channelList.get(position).getDeleteStatus().equals("1")) {
			icon_delete.setVisibility(View.VISIBLE);
			icon_delete.setOnClickListener(deleteClickListner);
		} else {
			icon_delete.setVisibility(View.GONE);
		}
//		item_text.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				Iterator<LuntanForumItemBean> iterator = channelList.iterator();
//				while (iterator.hasNext()) {
//					LuntanForumItemBean bean = iterator.next();
//
//					bean.setDeleteStatus((v.getTag() + "").equals("0") ? "1"
//							: "0");
//				}
//				notifyDataSetChanged();
//			}
//		});
		// if(isChanged){ //判断是否显示删除图标
		// icon_new.setVisibility(View.VISIBLE);
		// Animation shake = AnimationUtils.loadAnimation(
		// context, R.anim.anim);
		// shake.reset();
		// shake.setFillAfter(true);
		// view.startAnimation(shake);
		//
		// }else{
		// icon_new.setVisibility(View.GONE);
		// }
		return view;
	}

	/** 娣诲姞棰戦亾鍒楄〃 */
	public void addItem(LuntanForumItemBean channel) {
		channelList.add(channel);
		isListChanged = true;
		notifyDataSetChanged();
	}

	/** 鎷栧姩鍙樻洿棰戦亾鎺掑簭 */
	public void exchange(int dragPostion, int dropPostion) {
		holdPosition = dropPostion;
		LuntanForumItemBean dragItem = getItem(dragPostion);
		Log.d(TAG, "startPostion=" + dragPostion + ";endPosition="
				+ dropPostion);
		if (dragPostion < dropPostion) {
			channelList.add(dropPostion + 1, dragItem);
			channelList.remove(dragPostion);
		} else {
			channelList.add(dropPostion, dragItem);
			channelList.remove(dragPostion + 1);
		}
		isChanged = true;
		isListChanged = true;
		notifyDataSetChanged();
	}

	/** 鑾峰彇棰戦亾鍒楄〃 */
	public List<LuntanForumItemBean> getChannnelLst() {
		return channelList;
	}

	/** 璁剧疆鍒犻櫎鐨刾osition */
	public void setRemove(int position) {
		remove_position = position;
		notifyDataSetChanged();
	}

	/** 鍒犻櫎棰戦亾鍒楄〃 */
	public void remove() {
		channelList.remove(remove_position);
		remove_position = -1;
		isListChanged = true;
		notifyDataSetChanged();
	}

	/** 璁剧疆棰戦亾鍒楄〃 */
	public void setListDate(List<LuntanForumItemBean> list) {
		channelList = list;
	}

	/** 鑾峰彇鏄惁鍙 */
	public boolean isVisible() {
		return isVisible;
	}

	/** 鎺掑簭鏄惁鍙戠敓鏀瑰彉 */
	public boolean isListChanged() {
		return isListChanged;
	}

	/** 璁剧疆鏄惁鍙 */
	public void setVisible(boolean visible) {
		isVisible = visible;
	}

	/** 鏄剧ず鏀句笅鐨処TEM */
	public void setShowDropItem(boolean show) {
		isItemShow = show;
	}
}